<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */

  function quote_oanda_currency($code, $base = DEFAULT_CURRENCY) {

    $page = file('http://www.oanda.com/convert/fxdaily?value=1&redirected=1&exch=' . $code .  '&format=CSV&dest=Get+Table&sel_list=' . $base);

    $match = array();

    preg_match('/(.+),(\w{3}),([0-9.]+),([0-9.]+)/i', implode('', $page), $match);

    if (sizeof($match) > 0) {
      return $match[3];
    } else {
      return false;
    }
  }

  function quote_xe_currency($to, $from = DEFAULT_CURRENCY) {

    $page = file('http://www.xe.net/ucc/convert.cgi?Amount=1&From=' . $from . '&To=' . $to);

    $match = array();

    preg_match('/[0-9.]+\s*' . $from . '\s*=\s*([0-9.]+)\s*' . $to . '/', implode('', $page), $match);

    if (sizeof($match) > 0) {
      return $match[1];
    } else {
      return false;
    }
  }

class CurrenciesComponent
{
	function CurrenciesComponent()
	{
	}

	function action(&$params, $action)
	{
		return $this->query($params);
	}

	function query(&$params) 
	{
		$handle = get_class($this);
		$currency_id = (int)$params['currencyId'];
		if ($currency_id == 0) $currency_id = 1;
		
		$sqlQuery = "select currencies_id, title, code, value, DATE_FORMAT(last_updated, '%Y-%m-%d %H:%i:%s') as date_updated, " .
		'symbol_left, symbol_right, decimal_point, thousands_point, decimal_places ' .
		'from ' . TABLE_CURRENCIES . ' ' .
		'order by currencies_id';


		$value{$handle.'MetaData'}{'default_code'}= DEFAULT_CURRENCY;

		$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
		$i = 0;
		while($row = mysql_fetch_array($dataReturned))
		{
			$id = (int)$row['currencies_id'];
			$code = $row['code'];

			$currencyValue = (double)$row['value'];
			if ($code == DEFAULT_CURRENCY) $currencyValue = 1.0;

			$value{$handle}{$i}{'id'}= $id;
			$value{$handle}{$i}{'name'}= $row['title'];
			$value{$handle}{$i}{'code'}= $code;
			$value{$handle}{$i}{'value'}= $currencyValue;
			$value{$handle}{$i}{'date_updated'}= $row['date_updated'];
			$value{$handle}{$i}{'symbol_left'}= $row['symbol_left'];
			$value{$handle}{$i}{'symbol_right'}= $row['symbol_right'];
			$value{$handle}{$i}{'decimal_point'}= $row['decimal_point'];
			$value{$handle}{$i}{'thousands_point'}= $row['thousands_point'];
			$value{$handle}{$i}{'decimal_places'}= (int)$row['decimal_places'];
			
			if ($id == $currency_id)
			{
				$value{'CurrenciesComponentMetaData'}{'current_code'} = $row['code'];
			}
			if ($code != DEFAULT_CURRENCY)
			{
				$sqlQuery1 = 'select currencies_id ' .
				'from ' . TABLE_CURRENCIES . ' ' .
				'where currencies_id = ' . $id . ' and last_updated < \'' . date('Y-m-d') . '\' ';
				$dataReturned1 = mysql_query($sqlQuery1) or die(mysql_error());
				$i1 = 0;
				while($row1 = mysql_fetch_array($dataReturned1))
				{
					$i1++;
				}
				if ($i1 > 0)
				{
					//Refresh
					$rate = quote_oanda_currency($code, DEFAULT_CURRENCY);
					if (empty($rate)) $rate = quote_xe_currency($code, DEFAULT_CURRENCY);
					if (!empty($rate))
					{
						$sql = 'update ' . TABLE_CURRENCIES . ' set value = \'' . $rate . '\', last_updated = now() ' . 
						'where currencies_id = \'' . $id . '\'';
						mysql_query($sql) or die(mysql_error());
						$value{$handle}{$i}{'value'}= (double)$rate;
						$value{$handle}{$i}{'date_updated'}= date('Y-m-d H:i:s');
					}
				}
			}
			$i++;		}

		return $value;
	}
}
?>
